import axios from 'axios';
import type { RouteRecordNormalized } from 'vue-router';
import { AdminState } from '@/store/modules/admin/types';
import { AnyObject } from "@/types/global";

const url = '/adminapi/admin/';

export interface DataState {
  id: number | string;
  created_at?: string;
  updated_at?: string;
  avatar: string;
  full_avatar: string;
  username: string;
  nickname: string;
  phone: string;
  email: string;
  role_ids: string[];
  status: 1 | 2 | 90;
}

export interface LoginData {
  username: string;
  password: string;
}

export interface LoginRes {
  token: string;
}

export interface QueryParams extends Partial<AdminState> {
  page: number;
  limit: number;
}

export interface DataListRes {
  data: DataState[];
  total: number;
}

export function login(data: LoginData) {
  return axios.post<LoginRes>('/adminapi/auth/login', data);
}

export function logout() {
  return axios.post<LoginRes>('/adminapi/auth/logout');
}

/**
 * 个人中心
 */
export function getProfile() {
  return axios.post<AdminState>(`${url}profile`);
}

/**
 * 保存个人中心
 */
export function saveProfile(data: AdminState) {
  return axios.post(`${url}saveProfile`, data);
}

/**
 * 重置密码
 */
export function apiResetPwd(params: AnyObject) {
  return axios.get(`${url}resetPassword`, { params });
}

/**
 * 菜单
 */
export function getMenuList() {
  return axios.post<RouteRecordNormalized[]>(`${url}menu`);
}

/**
 * 列表
 */
export function apiList(params?: QueryParams) {
  return axios.get<DataListRes>(`${url}list`, { params });
}

/**
 * 详情
 */
export function apiInfo(params: AnyObject) {
  return axios.get(`${url}info`, { params });
}

/**
 * 保存
 */
export function apiSave(data: AdminState) {
  return axios.post(`${url}save`, data);
}

/**
 * 删除
 */
export function apiDel(params: AnyObject) {
  return axios.get(`${url}delete`, { params });
}

/**
 * 启用/禁用
 */
export function apiDisable(params: AnyObject) {
  return axios.get(`${url}handle`, { params });
}

/**
 * 设置密码
 */
export function apiSetPwd(params: AnyObject) {
  return axios.get(`${url}setPassword`, { params });
}

/**
 * 检查名称是否已存在
 */
export function apiCheckName(params: AnyObject) {
  return axios.get(`${url}checkLoginName`, { params });
}
